package com.nineclock.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nineclock.common.constant.SysCompanyContactConfigConstants;
import com.nineclock.common.entity.Result;
import com.nineclock.common.entity.UserInfo;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.utils.CurrentUserHolder;
import com.nineclock.system.dto.SysCompanyContactConfigDTO;
import com.nineclock.system.mapper.SysCompanyContactConfigMapper;
import com.nineclock.system.pojo.SysCompanyContactConfig;
import com.nineclock.system.service.SysCompanyContactConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class SysCompanyContactConfigServiceImpl extends ServiceImpl<SysCompanyContactConfigMapper, SysCompanyContactConfig> implements SysCompanyContactConfigService {

    @Override
    public Result add(SysCompanyContactConfigDTO dto) {

        UserInfo userInfo = CurrentUserHolder.get();
        if (userInfo == null) {
            return Result.error();
        }

        Long companyId = userInfo.getCompanyId();
        if (companyId == null || companyId == 0) {
            return Result.error();
        }

        SysCompanyContactConfig sysCompanyContactConfig = new SysCompanyContactConfig();
        sysCompanyContactConfig.setName(dto.getName());
        sysCompanyContactConfig.setType(dto.getType());
        sysCompanyContactConfig.setCompanyId(companyId);
        sysCompanyContactConfig.setFieldType("string");
        sysCompanyContactConfig.setStatus((short) 1);
        this.save(sysCompanyContactConfig);

        return Result.success();

    }


    /**
     * 思路：
     * 1.查询公司ID下的所有通讯录配置
     * 2.如果查询为NULL，则初始化默认的通讯录配置关联到改公司下存入表中
     * 3.如果查询不为NULL，则直接返回
     *
     * @return
     */
    @Override
    public Result findList() {

        List<SysCompanyContactConfig> list;

        UserInfo userInfo = CurrentUserHolder.get();
        if (userInfo == null) {
            return Result.error();
        }

        Long companyId = userInfo.getCompanyId();

        if (companyId == null || companyId == 0) {
            return Result.error();
        }

        list = this.list(Wrappers.<SysCompanyContactConfig>lambdaQuery()
                .eq(SysCompanyContactConfig::getCompanyId, companyId));

        if (list != null && list.size() > 0) {
            return Result.success(list);
        }

        companyId = 1L;

        list = this.list(Wrappers.<SysCompanyContactConfig>lambdaQuery()
                .eq(SysCompanyContactConfig::getCompanyId, companyId));

        list = list.stream().map(item -> {
            item.setCompanyId(userInfo.getCompanyId());
            return item;
        }).collect(Collectors.toList());


        this.saveBatch(list);

        return Result.success(list);


    }


    /**
     * 通讯录信息展示字段勾选
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Result update(Long id, Short status) {

        UserInfo userInfo = CurrentUserHolder.get();
        if (userInfo == null) {
            return Result.error();
        }

        boolean flag = this.update(Wrappers.<SysCompanyContactConfig>lambdaUpdate()
                .eq(SysCompanyContactConfig::getId, id)
                .set(SysCompanyContactConfig::getStatus, status));

        if (!flag) {
            return Result.error();
        }

        return Result.success();

    }

    /**
     * 删除通讯录字段
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Long id) {

        UserInfo userInfo = CurrentUserHolder.get();
        if (userInfo == null) {
            return Result.errorMessage(ResponseEnum.UNAUTHORIZED.getMessage());
        }

        Long companyId = userInfo.getCompanyId();
        if (companyId == null || companyId == 0) {
            return Result.errorMessage(ResponseEnum.COMPANY_NOT_FOUND.getMessage());
        }

        SysCompanyContactConfig db = this.getById(id);

        if (db == null) {
            return Result.errorMessage("该通讯录字段不存在");
        }


        if (SysCompanyContactConfigConstants.SYSCOMPANYCONTACTCONFIG_TYPE_FIXED.equals(db.getFieldType())) {
            return Result.errorMessage("该通讯录字段为固定字段不可删除");
        }

        this.removeById(id);


        return Result.success(ResponseEnum.SUCCESS);
    }

}
